﻿Public Class FormRk
    Private en As New entity_cp_ck
    Dim prod_ids As String
    Dim dtRkTemp As New DataTable
    Dim objAutoNumber As New clsAutoNumber
    Dim objCA As New clsCPCK
    Dim objProdInfo As New clsProdInfo
    Public usr As String
    Private Sub FormRk_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'setEmptyLOC()
        initDt()
        initWarehouseArea()
        txtProdId.Focus()
    End Sub

    Private Sub setEmptyLOC()
        Dim objMstList As New clsMstList
        loc.Items.Clear()
        Dim dt As DataTable = objMstList.getEmptyLoc("3")
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                loc.Items.Add(dt.Rows(i)("mst_value"))
            Next
        End If
    End Sub

    Public Sub initDt()
        dtRkTemp.Columns.Add("location")
        dtRkTemp.Columns.Add("area")
        dtRkTemp.Columns.Add("area_name")
        dtRkTemp.Columns.Add("prod_id")
        dtRkTemp.Columns.Add("prod_spec_name")
        dtRkTemp.Columns.Add("steel_seal_no")
        dtRkTemp.Columns.Add("prod_spec_id")
        dtRkTemp.Columns.Add("pgd_id")
    End Sub

    Private Sub initWarehouseArea()
        Dim dtWA As New DataTable()
        dtWA.Columns.Add("name")
        dtWA.Columns.Add("code")
        dtWA.Rows.Add("隔震成品库", "ZAGZ01")
        dtWA.Rows.Add("隔震成品库_A区", "ZAGZ01A")
        dtWA.Rows.Add("隔震成品库_B区", "ZAGZ01B")
        dtWA.Rows.Add("隔震成品库_C区", "ZAGZ01C")
        dtWA.Rows.Add("隔震成品库_D区", "ZAGZ01D")
        dtWA.Rows.Add("隔震成品库_E区", "ZAGZ01E")
        cmbWarehouseAreaCHP.DisplayMember = "name"
        cmbWarehouseAreaCHP.ValueMember = "code"
        cmbWarehouseAreaCHP.DataSource = dtWA
        cmbWarehouseAreaCHP.SelectedIndex = 0
    End Sub

    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If e.KeyChar = vbCr Then
            getData()
        End If
    End Sub

    Private Sub getData()
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            Dim dtt As DataTable = objProdInfo.getProdInfoByProdId(txtProdId.Text.Trim(), "15")
            If dtt.Rows.Count > 0 Then
                Dim dr As DataRow = dtt.Rows(0)
                txt_prod_spec.Text = dr("prod_spec_name")
                cmbWarehouseAreaLX.Items.Add(dr("area"))
                cmbWarehouseAreaLX.SelectedIndex = 0
                If String.IsNullOrEmpty(cmbWarehouseAreaLX.Text) = False Then
                    Dim dttx As DataTable = cmbWarehouseAreaCHP.DataSource
                    For index = 0 To dttx.Rows.Count - 1
                        If dttx.Rows(index)("name").ToString().Contains(cmbWarehouseAreaLX.Text) Then
                            cmbWarehouseAreaCHP.Text = dttx.Rows(index)("name").ToString()
                            cmbWarehouseAreaCHP.SelectedText = dttx.Rows(index)("name").ToString()
                        End If
                    Next
                End If
            Else
                txt_prod_spec.Text = String.Empty
                cmbWarehouseAreaLX.Text = String.Empty
                MsgBox("未查询到喷漆检测完成的此生产编号信息", vbYes, "提示")
            End If
        Else
            MsgBox("请扫描或输入条码值", MsgBoxStyle.Information, "成品入库")
        End If
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        getData()
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'If chkEmpty(loc) = False Then
        '   Exit Sub
        'End If
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            Dim dtxt As DataTable = objProdInfo.getProdInfoByProdId(txtProdId.Text.Trim(), "15")
            If dtxt.Rows.Count > 0 Then
                Dim dtt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "0")
                If dtt.Rows.Count = 0 Then
                    Dim dr As DataRow() = dtRkTemp.Select("prod_id='" & dtxt.Rows(0)("prod_id") & "'")
                    If dr.Length = 0 Then
                        dtRkTemp.Rows.Add(loc.Text.Trim(), cmbWarehouseAreaCHP.SelectedValue.ToString(), cmbWarehouseAreaCHP.SelectedText.Trim(),
                                          dtxt.Rows(0)("prod_id"), dtxt.Rows(0)("prod_spec_name"), dtxt.Rows(0)("steel_seal_no"),
                                          dtxt.Rows(0)("prod_spec_id"), dtxt.Rows(0)("pgd_id"))
                        prod_ids &= "'" & dtxt.Rows(0)("prod_id") & "',"
                        dgv.AutoGenerateColumns = False
                        dgv.DataSource = dtRkTemp
                        lblRowCount.Text = dgv.Rows.Count
                        clear()
                    Else
                        MsgBox("该标签号已在表格中存在！", vbYes, "提示")
                    End If
                Else
                    MsgBox("该生产编号已入库", vbYes, "提示")
                End If
            Else
                MsgBox("未查询到喷漆完成的此生产编号信息", vbYes, "提示")
            End If
        Else
            MsgBox("请扫描或输入条码值", vbYes, "提示")
        End If
    End Sub


    Private Sub btnRK_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        If dtRkTemp.Rows.Count > 0 Then
            If MsgBox("您确定要进行入库吗？", MsgBoxStyle.OkCancel, "提示") = MsgBoxResult.Ok Then
                Dim code As String = Now.ToString("yyyyMMdd") & "-" & objAutoNumber.getNextKeyNo("cp_rk_code", "cprkAutoNumber")
                Dim rtn As Integer = 0
                For index = 0 To dtRkTemp.Rows.Count - 1
                    setEN(code, dtRkTemp.Rows(index)("prod_id").ToString(), dtRkTemp.Rows(index)("area").ToString(), dtRkTemp.Rows(index)("area_name").ToString(),
                          dtRkTemp.Rows(index)("prod_spec_id").ToString(), dtRkTemp.Rows(index)("pgd_id").ToString())
                    Dim rtnn As Integer = objCA.intoCK(en)
                    If rtnn <> -2 Then
                        CType(Owner, FormMain).setDgvData()
                        rtn += 1
                    End If
                Next
                If rtn = dtRkTemp.Rows.Count Then
                    MsgBox("入库成功", vbYes, "提示")
                Else
                    MsgBox("入库成功数：" & rtn.ToString() & ",入库失败数：" & (dtRkTemp.Rows.Count - rtn).ToString() & ",请将失败的重新入库！", vbYes, "提示")
                End If
            Else
                'MsgBox(dtRkTemp.Rows(0)("area").ToString())
            End If
        Else
            MsgBox("请先添加喷漆检测完成的支座！", vbYes, "提示")
        End If
    End Sub

    Public Sub clear()
        txtProdId.Text = String.Empty
        txt_prod_spec.Text = String.Empty
        'loc.Text = ""
        'setEmptyLOC()
        cmbWarehouseAreaLX.Items.Clear()
        cmbWarehouseAreaCHP.SelectedIndex = 0
        txtProdId.Focus()
    End Sub
    Public Sub setEN(warehouse_in_code As String, prod_id As String, area As String, area_name As String, prod_spec_id As String, pgd_id As String)
        With en
            .factory_id = "TS"
            .status = "0"
            .into_ope = usr
            .wh_no = loc.Text.Trim()
            .prod_id = prod_id
            .area = area
            .area_name = area_name
            .warehouse_in_code = warehouse_in_code
            .prod_spec_id = prod_spec_id
            .pgd_id = pgd_id
        End With
    End Sub

End Class

Public Class entity_cp_ck
    Public sys_id As String
    Public factory_id As String
    Public wh_no As String
    Public prod_id As String
    Public into_time As DateTime
    Public out_time As DateTime
    Public status As String
    Public into_ope As String
    Public out_ope As String
    Public area As String
    Public area_name As String
    Public update_time As DateTime
    Public warehouse_in_code As String
    Public prod_spec_id As String
    Public pgd_id As String
End Class